<template>
  <div class="register_page">
    <van-nav-bar>
      <template #left>
        <van-icon @click="close" class="back" size="22" name="arrow-left" />
      </template>
    </van-nav-bar>
    <div class="content">
      <div class="title">欢迎注册Hello</div>
      <van-field
        v-model="tel"
        center
        clearable
        type="tel"
        placeholder="请输入手机号"
      >
      </van-field>
      <van-field
        v-model="password"
        :type="leixing"
        center
        clearable
        @input="change"
        placeholder="请输入密码"
      >
        <template #right-icon>
          <van-icon
          
            v-if="leixing == 'password'"
            @click="qiehuan_status"
            name="closed-eye"
          />
          <van-icon v-else @click="qiehuan_status" name="eye-o" />
        </template>
      </van-field>
      <van-button @click="register" :disabled="disabled" :class="register1" type="default"
        >注册</van-button
      >
    </div>
  </div>
</template>

<script>
import { Toast } from 'vant'
export default {
  name: "register",
  data() {
    return {
      tel: "",
      password: "",
      leixing: "password",
      register1: "register",
      disabled: true,
    };
  },
  methods: {
    change() {
      if (this.password.length >= 8) {
        this.register1 = "register1"
        this.disabled = false
      } else {
        this.register1 = "register"
        this.disabled = true
      }
    },
    close() {
      this.$router.go(-1);
    },
    qiehuan_status() {
      if (this.leixing == "password") {
        this.leixing = "number";
      } else {
        this.leixing = "password";
      }
    },
    yanzheng() {
      var myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
      if (!myreg.test(this.tel)) {
        return false;
      }
    },
    register() {
      // 手机号验证
      if (this.yanzheng() == false) {
        Toast.fail('请输入正确的手机号');
        return 
      }

      let tmp_obj = {
        tel: this.tel,
        password: this.password,
      };
      this.axios({
        method: "post",
        url: "http://localhost:3000/register",
        data: tmp_obj,
      })
        .then((res) => {
          console.log(res);
          if (res.data == "该账号已注册") {
            Toast.fail(res.data);
          } else {
            Toast.success(res.data);
            var tmp_obj = {
              tel: this.tel,
              password: this.password
            }
            this.$router.push({
              name: "login",
              params: tmp_obj
            })
          }
          
        })
        .catch((err) => {
          console.log(err);
          Toast.fail("注册失败");
        });
    },
  },
};
</script>

<style scoped>
.register_page {
  height: 100vh;
  background-color: white;
}

.back {
  color: black;
}

.content {
  padding: 50px;
}

.title {
  margin-bottom: 20px;
  font-size: 30px;
}

.van-cell {
  padding: 0px;
  height: 50px;
  font-size: 18px;
  border-bottom: 1px solid grey;
}

.register {
  height: 40px;
  background-color: rgb(247, 247, 193);
  margin-top: 10px;
  border-radius: 3px;
  width: 100%;
}

.register1 {
  height: 40px;
  background-color: rgb(218, 218, 9);
  margin-top: 10px;
  border-radius: 3px;
  width: 100%;
}
</style>